//
//  ManagerViewController.m
//  EasyToTesco
//
//  Created by mc on 16/3/21.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "ManagerViewController.h"
#import "HeaderFileForEasyToTesco.h"
#import "OrderDetailTableViewCell.h"

#import "ShowOnlyViewController.h"

//添加新地址
#import "AddAddressViewController.h"
#import "AFNetworking.h"
#import "ReadLoginData.h"
#import "GPDateView.h"

@interface ManagerViewController ()<UIAlertViewDelegate>;




@property (nonatomic, strong)UITableView *managertableview;
@property (nonnull, copy) NSString *str;
@property (strong, nonatomic)  UILabel *provinceLabel;
@property (strong, nonatomic)  UILabel *cityLabel;
@property (strong, nonatomic)  UILabel *areaLabel;

@property (weak, nonatomic) IBOutlet UILabel *city;

@property (weak, nonatomic) IBOutlet UILabel *prLL;

@property (weak, nonatomic) IBOutlet UILabel *areaCC;
@property (weak, nonatomic) IBOutlet UILabel *aaaaa;


@property (weak, nonatomic) IBOutlet UITextField *name;



@property (weak, nonatomic) IBOutlet UITextField *phone;

@property (weak, nonatomic) IBOutlet UITextField *address;


@property (weak, nonatomic) IBOutlet UITextField *youbian;


@end
#define DeviceHeight            [[UIScreen mainScreen] bounds].size.height
#define DeviceWidth             [[UIScreen mainScreen] bounds].size.width
#define Key_DistrictSelectProvince          @"DistrictSelectProvince"
#define Key_DistrictSelectProvinceCode      @"DistrictSelectProvinceCode"
#define Key_DistrictSelectProvinceSubCode   @"DistrictSelectProvinceSubCode"
#define Key_DistrictSelectProvinceSub       @"DistrictSelectProvinceSub"
#define Key_DistrictSelectCityCode          @"DistrictSelectCityCode"
#define Key_DistrictSelectCity              @"DistrictSelectCity"

@implementation ManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self getdata];
    
    [self addmanagertopUI];
  
}


- (IBAction)pickerButton:(id)sender {
    [self pickerView];
}



- (void)pickerView{
    
    GPDateView * dateView = [[GPDateView alloc] initWithFrame:CGRectMake(0, DeviceHeight-250, DeviceWidth, 250) Data:nil];
    
    [dateView showPickerView];
    
    dateView.ActionDistrictViewSelectBlock = ^(NSString *desStr,NSDictionary *selectDistrictDict){
        self.aaaaa.text = [selectDistrictDict objectForKey:Key_DistrictSelectProvince];
        self.prLL.text = [selectDistrictDict objectForKey:Key_DistrictSelectCity];
        self.areaCC.text = [selectDistrictDict objectForKey:Key_DistrictSelectProvinceSub];
        
    };

    
    
}


- (void)addmanagertopUI {
    
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"管理收货地址";
    
    UIView *lastView = [[UIView alloc] initWithFrame:CGRectMake(0, HIGHT-60, WEITH, 60)];
    lastView.backgroundColor = [UIColor grayColor];
    [self.view addSubview:lastView];
    
    UIButton *addbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addbtn.frame = CGRectMake((WEITH-70)/2, 10, 140, 40);
    addbtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [addbtn setTitle:@"添加收货地址" forState:UIControlStateNormal];
    [addbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [addbtn addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    addbtn.backgroundColor = [UIColor greenColor];
    [lastView addSubview:addbtn];
    

}

- (void)buttonAction{

 
    
    if (self.name.text.length == 0 || self.phone.text.length == 0|| self.address.text.length == 0|| self.youbian.text.length == 0) {

        
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请填写完整信息" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alview.tag = 123306;
        [alview show];
        
        
    }else{
        
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
        mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        
        
        
        NSDictionary *param = @{@"username":[NSString stringWithFormat:@"%@", self.str],
                                @"name":[NSString stringWithFormat:@"%@", self.name.text],
                                @"phone":[NSString stringWithFormat:@"%@", self.phone.text],
                                @"address":[NSString stringWithFormat:@"%@", self.address.text],
                                @"zt":[NSString stringWithFormat:@"%@", @1],
                                @"province":[NSString stringWithFormat:@"%@", self.aaaaa.text],
                                @"city":[NSString stringWithFormat:@"%@", self.city.text],
                                @"county":[NSString stringWithFormat:@"%@", self.areaCC.text],
                                @"postcode":[NSString stringWithFormat:@"%@", self.youbian.text],};
        
        [mgr POST:@"http://www.ylgw8.com/AppUser/Adddz" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            NSLog(@"%@", string);
            
            NSDictionary *dict = [self toArrayOrNSDictionary:responseObject];
            
            if ([dict[@"messange"] isEqualToString:@"成功"]) {
                UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"添加失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                alview.tag = 123304;
                [alview show];
                
                
            }else{
                
                UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"添加成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                alview.tag = 123305;
                [alview show];
                
            }
        
            
        } failure:^(AFHTTPRequestOperation *operation,NSError *error)
         {
             //         operation.response.statusCode;//404 401之类
             //         NSLog(@"数据请求错误原因:%@", error);
             //         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
             NSLog(@"失败");
         }];
    
    }
 
    
}

-(void)namePass{
    
    
}



- (void)getdata {
    // 获取当前账号
    NSDictionary *dic = [ReadLoginData readLoginData];
    if (dic.allKeys.count == 0) {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未登录,请登录后再次操作" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        alview.tag = 123303;
        [alview show];
        
    }else {
        

//        [self addgetaddressdatasources: [dic[@"data"] objectForKey:@"username"]];
        self.str = [dic[@"data"] objectForKey:@"username"];
    }
    
}



- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}
- (void)alertViewCancel:(UIAlertView *)alertView{
    
    if (alertView.tag == 123303 || alertView.tag == 123305) {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }else{
        
    }
  
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    [self gobackClick];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
